<template>
  <div class="form">
    <el-form label-position="top" label-width="80px" :model="menuForm">
      <el-form-item label="父菜单">
        <el-input v-model="parentMenuName" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="菜单名称">
        <el-input v-model="menuForm.title"></el-input>
      </el-form-item>
      <el-form-item label="路径">
        <el-input v-model="menuForm.path"></el-input>
      </el-form-item>
      <el-form-item label="代号">
        <el-input v-model="menuForm.code"></el-input>
      </el-form-item>
      <el-form-item label="图标">
        <el-input v-model="menuForm.icon"></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="menuForm.seq"  :min="1"  label="排序" controls-position="right"></el-input-number>
      </el-form-item>
      <el-form-item label="有无子菜单">
        <el-switch
          v-model="menuForm.hasChildren"
          active-value="1"
          inactive-value="0">
        </el-switch>
      </el-form-item>
      <el-form-item label="是否隐藏">
        <el-switch
          v-model="menuForm.hidden"
          active-value="1"
          inactive-value="0">
        </el-switch>
      </el-form-item>
      <div style="text-align: right;">
        <el-form-item>
          <el-button type="primary" @click="onAdd">保存</el-button>
          <el-button @click="onClose">取消</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import api from '@/api'

export default {
  name: 'menu-add',
  props: {
    parentId: {
      type: Number
    }
  },
  data () {
    return {
      menuForm: {
        icon: 'puzzle-piece',
        hidden: 0,
        hasChildren: 0,
        seq: 0
      },
      parentMenuName: '根节点'
    }
  },
  mounted () {
    this.getMenuById()
  },
  methods: {
    onAdd () {
      var param = this.menuForm
      param.parentId = this.parentId
      api.sysMenuInsert(param).then(res => {
        this.$message({ type: 'info', message: '已添加' })
        this.onClose()
        this.$emit('refresh')
      })
    },
    getMenuById () {
      if (this.parentId === 0) {
        return
      }
      var param = { id: this.parentId }
      api.sysMenuFindById(param).then(res => {
        this.parentMenuName = res.title
      })
    },
    onClose () {
      this.$emit('closeDrawer')
    }
  }
}
</script>

<style lang="scss">
  .form {
    padding: 10px;
  }
</style>
